var date = {
  /**
   * 获取当前年的周数
   * @returns {number}
   */
  getWeekOfYear: function () {
    var today = new Date()
    // var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
    var firstDay = new Date(today.getFullYear(), 0, 1)
    var dayOfWeek = firstDay.getDay()
    // console.log("当前年的第一天（1月1号）是星期："+dayOfWeek);
    var spendDay = 1
    if (dayOfWeek !== 0) {
      spendDay = 7 - dayOfWeek + 1
    }
    firstDay = new Date(today.getFullYear(), 0, 1 + spendDay)
    var d = Math.ceil((today.valueOf() - firstDay.valueOf()) / 86400000)
    var result = Math.ceil(d / 7)
    return result + 1
  },
  // 获取当前月份
  getMonthOfYear: function () {
    var now = new Date()
    var monthn = now.getMonth() + 1
    return monthn
  },
  /**
   * 获取指定日期的开始结束时间
   * @param weekIndex
   * @returns {{startDate: string, endDate: string}}
   */
  getSpecifyWeekStartToEndTime: function (weekIndex) {
    var now = new Date()
    var firstMonday = new Date(now.getFullYear(), 0, 1)
    while (firstMonday.getDay() !== 1) {
      firstMonday.setDate(firstMonday.getDate() + 1)
    }
    var monday = new Date(firstMonday)
    monday.setDate(firstMonday.getDate() + (weekIndex - 1) * 7)
    var sunday = new Date(firstMonday)
    sunday.setDate(firstMonday.getDate() + weekIndex * 7 - 1)
    var mondayStr = monday.getFullYear() + '年' + (monday.getMonth() + 1) + '月' + monday.getDate() + '日'
    var sundayStr = sunday.getFullYear() + '年' + (sunday.getMonth() + 1) + '月' + sunday.getDate() + '日'
    var startMon = (monday.getMonth() + 1) + '月' + monday.getDate() + '日'
    var endMon = (sunday.getMonth() + 1) + '月' + sunday.getDate() + '日'
    return {'startDate': mondayStr, 'endDate': sundayStr, 'startMon': startMon, 'endMon': endMon}
  },
  /**
   * 获取本周开始结束时间
   * @returns {{startDate: string, endDate: string}}
   */
  getWeekInfoOfThisWeek () {
    var now = new Date()
    var nowTime = now.getTime()
    var day = now.getDay()
    var oneDayTime = 24 * 60 * 60 * 1000
    var MondayTime = nowTime - (day - 1) * oneDayTime // 显示周一
    var SundayTime = nowTime + (7 - day) * oneDayTime // 显示周日
    var monday = new Date(MondayTime)
    var sunday = new Date(SundayTime)
    var mondayStr = monday.getFullYear() + '年' + (monday.getMonth() + 1) + '月' + monday.getDate() + '日'
    var sundayStr = sunday.getFullYear() + '年' + (sunday.getMonth() + 1) + '月' + sunday.getDate() + '日'
    return {'startDate': mondayStr, 'endDate': sundayStr}
  },
  /**
   * 获取上周开始结束时间
   * @returns {{startDate: string, endDate: string}}
   */
  getWeekInfoOfLastWeek () {
    var now = new Date()
    var nowTime = now.getTime()
    var day = now.getDay()
    var oneDayTime = 24 * 60 * 60 * 1000
    var MondayTime = nowTime - (7 + day - 1) * oneDayTime // 显示周一
    var SundayTime = nowTime - oneDayTime * day // 显示周日
    var monday = new Date(MondayTime)
    var sunday = new Date(SundayTime)
    var mondayStr = monday.getFullYear() + '年' + (monday.getMonth() + 1) + '月' + monday.getDate() + '日'
    var sundayStr = sunday.getFullYear() + '年' + (sunday.getMonth() + 1) + '月' + sunday.getDate() + '日'
    return {'startDate': mondayStr, 'endDate': sundayStr}
  },
  // 时间转换
  getInforDate (dates) {
    var date = ''
    if (dates !== null && dates !== undefined && dates !== '') {
      if (dates.constructor !== Date) {
        let datetimes = dates.replace(/-/g, '/')
        dates = new Date(datetimes)
      }
      var year = dates.getFullYear()
      var month = (dates.getMonth() + 1 < 10) ? '0' + (dates.getMonth() + 1) : dates.getMonth() + 1
      var day = dates.getDate() < 10 ? '0' + dates.getDate() : dates.getDate()
      var hour = dates.getHours() < 10 ? '0' + dates.getHours() : dates.getHours()
      var min = dates.getMinutes() < 10 ? '0' + dates.getMinutes() : dates.getMinutes()
      var sec = dates.getSeconds() < 10 ? '0' + dates.getSeconds() : dates.getSeconds()
      date = year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec
    }
    return date
  },
  // 时间格式化
  formatDate (date1, fmt) {
    if (date1 === null || date1 === undefined || date1 === '') {
      fmt = ''
    } else {
      let timeStr = date1.replace(/-/g, '/')
      var date = new Date(timeStr)
      let o = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        'h+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'S': date.getMilliseconds() // 毫秒
      }
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      for (var k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
        }
      }
    }
    return fmt
  }
}

export default date
